package com.hantanger.weblog.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.hantanger.weblog.model.Category;
import com.hantanger.weblog.model.Setting;
import com.hantanger.weblog.util.ClassFactory;
import com.hantanger.weblog.util.DaoHelper;

public class CategoryDao {
	
	public SettingDao getSettingDao(){
		return ClassFactory.getInstance(SettingDao.class);
	}

	public void insert(Category category) {
		long parentClassId = category.getParentId();
		Category existCat = getCategoryByName(category.getName(), parentClassId);
		if(existCat != null)
			return;
		
		Category parentCategory = null;
		if(category.getParentId() > 0){
			parentCategory = getCategoryById(parentClassId);
			if(parentCategory == null){
				return;
			}
		}
		update(category);
		
		if(parentCategory != null){
			String subCategorys = parentCategory.getSubCategorys();
			if(subCategorys == null){
				subCategorys = "";
			}
			parentCategory.setSubCategorys(category.getId() + "," + subCategorys);
			update(parentCategory);
		}
		
		Setting setting = getSettingDao().getDefaultSetting();
		if(parentClassId == 0 && category.getId() > 0){
			String classIdStr = setting.getGlobalClassIdStr();
			if(classIdStr == null){
				classIdStr = "";
			}
			setting.setGlobalClassIdStr(category.getId() + "," + classIdStr);
			getSettingDao().update(setting);
		}
	}

	public void update(Category category) {
		DaoHelper.save(category);
	}

	public void delete(Long id) {
		DaoHelper.delete(Category.class, id);
	}
	
	public Category getCategoryById(Long id) {
		return DaoHelper.get(Category.class, id);
	}

	public List<Category> getAllCategorys() {
		List<Category> categorys = DaoHelper.getLoader().type(Category.class).list();
		Map<String, Category> catMap = new HashMap<String, Category>();
		for (Category cat : categorys) {
			catMap.put(cat.getId() + "", cat);
		}

		//排序
		List<Category> result = new ArrayList<Category>(categorys.size());
		Setting setting = getSettingDao().getDefaultSetting();
		String globalClassIdStr = setting.getGlobalClassIdStr();
		
		if(globalClassIdStr == null){
			return result;
		}
		
		String[] firstClassCatIds = globalClassIdStr.split(",");
		for (String firstClassCatId : firstClassCatIds) {
			Category firstClassCat = catMap.get(firstClassCatId);
			if (firstClassCat == null) {
				continue;
			}
			result.add(firstClassCat);
			String secondClassCatStr = firstClassCat.getSubCategorys();
			if (StringUtils.isNotBlank(secondClassCatStr)) {
				String[] secondClassCatIds = secondClassCatStr.split(",");
				for (String secondClassCatId : secondClassCatIds) {
					Category secondClassCat = catMap.get(secondClassCatId);
					if (secondClassCat != null) {
						result.add(secondClassCat);
						//firstClassCat.setAllCount(firstClassCat.getAllCount() + secondClassCat.getAllCount());
						//firstClassCat.setPublicCount(firstClassCat.getPublicCount() + secondClassCat.getPublicCount());
						//firstClassCat.setPublishedCount(firstClassCat.getPublishedCount() + secondClassCat.getPublishedCount());
					}
				}
			}
		}
		return result;
	}
	
	public Category getCategoryByName(String categoryName, long parentId){
		List<Category> categorys = getAllCategorys();
		for(Category category : categorys){
			if(categoryName.equalsIgnoreCase(category.getName())
					&& category.getParentId() == parentId){
				return category;
			}
		}
		return null;
	}
	
	public Category getDefCategory(){
		Category defCategroy = getCategoryByName(Category.DEFCATEGORYNAME, 0L);
		if(defCategroy == null){
			defCategroy = new Category();
			defCategroy.setName(Category.DEFCATEGORYNAME);
			insert(defCategroy);
		}
		return defCategroy;
	}
	
	public Category getCategoryByParentName(String catName, String parentName){
		if(parentName == null){
			return getCategoryByName(catName, 0);
		}
		List<Category> categorys = getAllCategorys();
		Category parentCat = null;
		for(Category category : categorys){
			if(parentCat == null && category.getParentId() == 0 && category.getName().equals(parentName)){
				parentCat = category;
			}
			if(parentCat != null && category.getParentId() == parentCat.getId() && category.getName().equals(catName)){
				return category;
			}
		}
		return null;
	}
	
}
